<?php
    Class Solution{
        /**
         * 448. 找到所有数组中消失的数字
         * @param Integer[] $nums
         * @return Integer[]
         */
        function findDisappearedNumbers448($nums) {
            $len = count($nums);
            foreach ($nums as $key => &$num) {
                if(!isset($nums[$num+$len])){
                    $nums[$num+$len] = $num;
                    unset($nums[$key]);
                }
            }
            for($i = 1; $i <= $len; $i++){
                if($nums[$i]){
                    unset($nums[$i]);
                }
                if(!isset($nums[$i+$len])){
                    $nums[$i+$len] = $i;
                }else{
                    unset($nums[$i+$len]);
                }
            }

            return $nums;
        }


        /**
         * 455. 分发饼干
         * @param Integer[] $g
         * @param Integer[] $s
         * @return Integer
         */
        function findContentChildren455($g, $s) {
            $satis = 0;
            sort($g);
            sort($s);
            $i = 0;$j = 0;
            while($i < count($g) && $j < count($s)) {
                if($g[$i] <= $s[$j]){
                    $i++;$j++;
                    $satis++;
                }
                else{
                    $j++;
                }
            }

            return $satis;
        }

        /**
         * 461. 汉明距离
         * @param Integer $x
         * @param Integer $y
         * @return Integer
         */
        function hammingDistance461($x, $y) {
            $count = 0;
            while($x > 0 || $y > 0){
                if($x % 2 != $y % 2 ){
                    $count++;
                }
                $x = intval($x / 2);
                $y = intval($y / 2);
            }

            return $count;
        }

        /**
         * 463. 岛屿的周长
         * @param Integer[][] $grid
         * @return Integer
         */
        function islandPerimeter463($grid) {
            $di = 0;
            $bian = 0;
            foreach($grid as $x1 => $y1) {
                foreach($y1 as $x2 => $y2) {
                    if($grid[$x1][$x2]){
                        $di++;
                        if(isset($grid[$x1+1][$x2]) && $grid[$x1+1][$x2] == 1){
                            $bian++;
                        }
                        if(isset($grid[$x1][$x2+1]) && $grid[$x1][$x2+1] == 1){
                            $bian++;
                        }
                    }
                }
            }

            return (4 * $di) - (2 * $bian);
        }

        /**
         * 476. 数字的补数
         * @param Integer $num
         * @return Integer
         */
        function findComplement476($num) {
            IF($num == 1){
                return 0;
            }
            $time = intval(log($num, 2))+1;

            return 2**$time-1-$num;
        }


        /**
         * 482. 密钥格式化
         * @param String $s
         * @param Integer $k
         * @return String
         */
        function licenseKeyFormatting482($s, $k) {
            $str   = '';
            $count = 0;
            for($i = strlen($s)-1; $i >= 0; $i--){
                if($count == $k && $s[$i] != '-'){
                    $str = '-' . $str;
                    $count = 0;
                }
                if($s[$i] != '-'){
                    $str = strtoupper($s[$i]) . $str;
                    $count++;
                }
            }

            return $str;
        }

        /**
         * 485. 最大连续 1 的个数
         * @param Integer[] $nums
         * @return Integer
         */
        function findMaxConsecutiveOnes485($nums) {
            $max   = 0;
            $count = 0;
            foreach($nums as $num) {
                if($num == 0){
                    if($count >= $max){
                        $max   = $count;
                    }
                    $count = 0;
                    continue;
                }
                $count++;
            }

            return $max > $count?$max:$count;
        }

        /**
         * 492. 构造矩形
         * @param Integer $area
         * @return Integer[]
         */
        function constructRectangle492($area) {
            $i = sqrt($area);
            if(intval($i) == $i){
                return [$i, $i];
            }
            $i = intval($i)+1;
            while($area % $i != 0){
                $i++;
            }

            return [$i, $area/$i];
        }

        /**
         * 495. 提莫攻击
         * @param Integer[] $timeSeries
         * @param Integer $duration
         * @return Integer
         */
        function findPoisonedDuration495($timeSeries, $duration) {
            $count   = 0;
            $endTime = 0;
            foreach($timeSeries as $time) {
                $count = $count + $duration;
                if($time < $endTime){
                    $count = $count - ($endTime - $time);
                }
                $endTime = $time + $duration;
            }

            return $count;
        }

        /**
         * 496. 下一个更大元素 I
         * @param Integer[] $nums1
         * @param Integer[] $nums2
         * @return Integer[]
         */
        function nextGreaterElement496($nums1, $nums2) {
            $map   = [];
            $stack = [];
            foreach ($nums2 as $num) {
                while(!empty($stack) && $num > end($stack)){
                    $small = array_pop($stack);
                    $map[$small] = $num;
                }
                $stack[] = $num;
            }
            $array = [];
            foreach($nums1 as $num) {
                $array[] = $map[$num]?:-1;
            }

            return $array;
        }



    }


    $solution = new Solution();
    $timeSeries = [0,1,2,3,4,5,6,7,8,9,10,];
    $duration = 1;
    print_r($solution->findPoisonedDuration495($timeSeries, $duration));

?>